package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
 import ${superControllerClassPackage};
</#if>
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
* <p>
* ${table.comment!} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
@Api(tags = "${(table.comment?substring(0,(table.comment)?length - 1))!}API")
@Validated
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass} {
<#else>public class ${table.controllerName} {
    </#if>

    @Autowired
    ${table.serviceImplName} ${table.serviceName?substring(1)?uncap_first};

    @ApiOperation(value = "分页列表")
    @GetMapping
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页条数", defaultValue = "10", dataType = "int", paramType = "query")
    })
    public ApiResult<${entity}> page(@NotNull(message = "当前页不能为空") Integer pageNo, @NotNull(message = "页条数不能为空") Integer pageSize) {
        IPage<${entity}> page = ${table.serviceName?substring(1)?uncap_first}.page(new Page<>(pageNo, pageSize));
        return ApiResult.success(page);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public ApiResult save(@RequestBody ${entity} ${entity?substring(1)?uncap_first}) {
        boolean save = ${table.serviceName?substring(1)?uncap_first}.save(${entity?substring(1)?uncap_first});
        if (!save){
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<${entity}> detail(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        ${entity} ${entity?substring(1)?uncap_first} = ${table.serviceName?substring(1)?uncap_first}.getById(id);
        if (${entity?substring(1)?uncap_first} == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
       return ApiResult.success(${entity?substring(1)?uncap_first});
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public ApiResult update(@RequestBody ${entity} ${entity?substring(1)?uncap_first}) {
        boolean update = ${table.serviceName?substring(1)?uncap_first}.updateById(${entity?substring(1)?uncap_first});
        if (!update) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @DeleteMapping(/{id})
    public ApiResult del(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        boolean del = ${table.serviceName?substring(1)?uncap_first}.removeById(id);
        if (!del) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    /**
    * 查询条件简化
    * @return
    */
    public LambdaQueryWrapper<${entity}> query(){
        return new LambdaQueryWrapper<${entity}>();
    }

}
</#if>
